package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.Movements;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VisitorsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;

    //发布动态
    @PostMapping
    //@NoAuthorization
    public ResponseEntity<Void> saveMovements(@RequestParam("textContent") String textContent,
                                              @RequestParam("location") String location,
                                              @RequestParam("longitude") String longitude,
                                              @RequestParam("latitude") String latitude,
                                              @RequestParam("imageContent") MultipartFile[] files) {

        try {
            String publishId = movementsService.saveMovements(textContent, location, longitude, latitude, files);
            if (StringUtils.isNotEmpty(publishId)) {

                //向RocketMQ发送消息:type 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
                quanziMQService.sendMsg(1,publishId);

                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //查询好友动态
    @GetMapping
    public ResponseEntity<PageResult> queryFriendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                             @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        try {
            PageResult pageResult = movementsService.queryFriendPublishList(page, pageSize);

            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //查询推荐动态
    @GetMapping("/recommend")
    public ResponseEntity<PageResult> queryRecommendList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                         @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        try {
            PageResult pageResult = movementsService.queryRecommendPublishList(page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //点赞
    @GetMapping("/{publishId}/like")
    public ResponseEntity<Long> likeComment(@PathVariable String publishId) {

        try {
            Long count = movementsService.likeComment(publishId); //获取点赞数
            if (null != count) { //点赞成功

                //向RocketMQ发送消息:type 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
                quanziMQService.sendMsg(3,publishId);

                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //取消点赞
    @GetMapping("/{publishId}/dislike")
    public ResponseEntity<Long> dislikeComment(@PathVariable String publishId) {

        try {
            Long count = movementsService.dislikeComment(publishId);
            if (null != count) { //取消点赞成功

                //向RocketMQ发送消息:type 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
                quanziMQService.sendMsg(6,publishId);

                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //喜欢
    @GetMapping("/{publishId}/love")
    public ResponseEntity<Long> loveComment(@PathVariable String publishId) {

        try {
            Long count = movementsService.loveComment(publishId); //获取喜欢数
            if (null != count) { //喜欢成功

                //向RocketMQ发送消息:type 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
                quanziMQService.sendMsg(4,publishId);

                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //取消喜欢
    @GetMapping("/{publishId}/unlove")
    public ResponseEntity<Long> unloveComment(@PathVariable String publishId) {

        try {
            Long count = movementsService.unloveComment(publishId);
            if (null != count) { //取消喜欢成功

                //向RocketMQ发送消息:type 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
                quanziMQService.sendMsg(7,publishId);

                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //查询单条动态
    @GetMapping("/{id}")
    public ResponseEntity<Movements> queryMovementsById(@PathVariable("id") String publishId) {

        try {
            Movements movements = movementsService.queryMovementsById(publishId);

            if (null!=movements){

                //向RocketMQ发送消息:type 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
                quanziMQService.sendMsg(2,publishId);

                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //谁看过我
    @GetMapping("/visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList(){
        try {
            List<VisitorsVo> list = this.movementsService.queryVisitorsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //查询用户自己的动态
    @GetMapping("/all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId) {
        try {
            PageResult pageResult = this.movementsService.queryAlbumList(userId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
